const mongoose = require('mongoose')
mongoose.Promise = require('bluebird')

module.exports.connect = () => {
  return new Promise((resolve, reject) => {
    let conn = mongoose.connection
    conn.on('error', (err) => reject(err))
    conn.once('open', () => {
      console.log(`the mongodb is connection at mongodb://localhost/sznzkj`)
      resolve()
    })
    conn.on('disconnected', () => {
      console.log('mongodb the connection is closed')
    })
    process.on('SIGINT', () => {
      conn.close(() => {
        console.log('mongodb the connect is closed ')
        process.exit(0)
      })
    })
    mongoose.connect('mongodb://localhost/sznzkj', {useMongoClient:true})
  })
}